﻿using Assignment_3.Classes;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Assignment_3.Feature___Booking
{
    public partial class ViewBooking : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int bookingID = Convert.ToInt32(Request.QueryString["booking"]);
                int roomID = Convert.ToInt32(Request.QueryString["room"]);

                Booking b = DatabaseManager.ViewBookingByBID(bookingID);

                if (b != null)
                {
                    lblBookingID.Text = bookingID.ToString();
                    DateTime checkIn = b.Check_In_Date;
                    lblCheckIn.Text = String.Format("{0:yyyy-MM-dd}", checkIn);
                    DateTime checkOut = b.Check_Out_Date;
                    lblCheckOut.Text = String.Format("{0:yyyy-MM-dd}", checkOut);
                    lblRooms.Text = b.No_Of_Rooms.ToString();
                    lblAdults.Text = b.No_Of_Adults.ToString();
                    lblChildren.Text = b.No_Of_Children.ToString();
                }

                Room r = DatabaseManager.ViewRoomByRID(roomID);

                if (r != null)
                {
                    lblRoomType.Text = r.Room_Type;
                    lblBedType.Text = r.Bed_Type;
                    lblRoomsStatus.Text = r.Status;
                }

                Guest g = DatabaseManager.ViewGuestByBID(bookingID);

                if (g != null)
                {
                    lblGuestName.Text = g.Name;
                    lblPhoneNo.Text = g.Phone.ToString();
                    lblEmail.Text = g.Email;
                    lblAddress.Text = g.Address;
                    lblPostalCode.Text = g.PostalCode.ToString();
                    lblCountry.Text = g.Country;
                    lblPaymentType.Text = g.PaymentType;
                }

                if (g.PaymentType != null)
                {
                    if (g.PaymentType.Equals("Credit Card", StringComparison.OrdinalIgnoreCase))
                    {
                        Payment p = DatabaseManager.ViewPaymentByBID(bookingID);

                        lblCardholderName.Text = p.CardholderName;
                        lblCreditCardNo.Text = p.CreditCardNo;
                        //DateTime expiryDate = p.ExpiryDate;
                        //lblExpiryDate.Text = String.Format("{0:yyyy-MM-dd}", expiryDate);
                        lblExpiryDate.Text = p.ExpiryDate.ToString("yyyy-MM-dd");

                        lblCardholder.Visible = true;
                        lblCreditCard.Visible = true;
                        lblExpiration.Visible = true;

                        lblCardholderName.Visible = true;
                        lblCreditCardNo.Visible = true;
                        lblExpiryDate.Visible = true;
                    }
                    else if (g.PaymentType.Equals("Cash", StringComparison.OrdinalIgnoreCase))
                    {
                        lblCardholder.Visible = false;
                        lblCreditCard.Visible = false;
                        lblExpiration.Visible = false;

                        lblCardholderName.Visible = false;
                        lblCreditCardNo.Visible = false;
                        lblExpiryDate.Visible = false;
                    }
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int bookingID = Convert.ToInt32(lblBookingID.Text);
            int roomID = Convert.ToInt32(Request.QueryString["room"]);
            Response.Redirect("UpdateBooking.aspx?booking=" + bookingID + "&room=" + roomID);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("RetrieveBookings.aspx");
        }
    }
}